package com.qingxin.wuye.action;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.qingxin.sys.action.BaseAction;
import com.qingxin.sys.mvc.PageParam;
import com.qingxin.sys.mvc.SysError;
import com.qingxin.sys.support.SystemCfg;
import com.qingxin.wuye.entity.WyBuilding;
import com.qingxin.wuye.entity.WyGarden;
import com.qingxin.wuye.service.WyBuildingService;
import com.qingxin.wuye.service.WyGardenService;

@Controller
@RequestMapping("/building")
public class BuildingAction extends BaseAction {

	@Autowired
	private WyBuildingService buildingService;
	
	@Autowired
	private WyGardenService gardenService;
	
	
	@RequestMapping(value="/getBuildingData.json", method={RequestMethod.GET, RequestMethod.POST})
	@ResponseBody
	public Object getBuildingData(String gardenid, HttpServletRequest request){
		return buildingService.getUITreeData(gardenid, getLoginUser(request), false);
	}
	
	
	/**
	 * 列表页
	 * @param model
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/list.do", method=RequestMethod.GET)
	public String list(ModelMap model, HttpServletRequest request){
		return "wuye/building/building_list";
	}
	
	/**
	 * 获取列表分页数据
	 * @param datatype
	 * @param leftdata
	 * @param buildingName
	 * @param pageParam
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/listdata.json" , method={RequestMethod.GET, RequestMethod.POST})
	@ResponseBody
	public Object listdata(String datatype, String leftdata, String buildingName, PageParam pageParam, HttpServletRequest request){
		String companyid = null;
		String gardenid = null;
		if(SystemCfg.UI_DATATYPE_COMPANY.equalsIgnoreCase(datatype)){
			companyid = leftdata;
		}else if(SystemCfg.UI_DATATYPE_GARDEN.equalsIgnoreCase(datatype)){
			gardenid = leftdata;
		}
		
		return buildingService.list(companyid, gardenid, buildingName, pageParam, getLoginUser(request));
	}
	
	
	/**
	 * 添加信息
	 * @param model
	 * @param gardenid
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/add.do", method=RequestMethod.GET)
	public String add(ModelMap model, @RequestParam(value="leftdata")String gardenid, HttpServletRequest request){
		model.put("gardenid", gardenid);
		
		WyGarden garden = gardenService.get(gardenid);
		model.put("garden", garden);
		
		return "wuye/building/building_add";
	}
	
	
	/**
	 * 保存
	 * @param building
	 * @param result
	 * @param gardenid
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/save.json", method=RequestMethod.POST)
	@ResponseBody
	public Object save(@Valid WyBuilding building, BindingResult result, String gardenid, HttpServletRequest request){
		if(result.hasErrors()){
			return getSysErrors(result);
		}
		
		WyGarden garden = new WyGarden(gardenid);
		building.setWyGarden(garden);
		
		SysError info = buildingService.add(building, getLoginUser(request), request);
		return info;
	}

	
	/**
	 * 修改
	 * @param model
	 * @param buildingid
	 * @return
	 */
	@RequestMapping(value="/edit.do", method=RequestMethod.GET)
	public String edit(ModelMap model, String buildingid){
		WyBuilding building = buildingService.get(buildingid);
		model.put("building", building);
		
		WyGarden garden = gardenService.get(building.getWyGarden().getGardenid());
		model.put("garden", garden);
		
		return "wuye/building/building_edit";
	}
	
	
	/**
	 * 保存修改
	 * @param building
	 * @param result
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/update.json", method=RequestMethod.POST)
	@ResponseBody
	public Object update(@Valid WyBuilding building, BindingResult result, HttpServletRequest request){
		if(result.hasErrors()){
			return getSysErrors(result);
		}
		
		SysError info = buildingService.update(building, getLoginUser(request), request);
		return info;
	}
	
	
	/**
	 * 删除
	 * @param buildingid
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/delete.json", method=RequestMethod.POST)
	@ResponseBody
	public Object delete(String[] buildingid, HttpServletRequest request){
		if(buildingid==null || buildingid.length==0){
			return new SysError("没有选择任何信息.");
		}
		return buildingService.delete(buildingid, getLoginUser(request), request);
	}
	
	
	
}
